from lib.data.exts import db
import time

class EntityBase(object):
    def to_json(self):
        fields = self.__dict__.copy()
        if "_sa_instance_state" in fields:
            del fields["_sa_instance_state"]
        return fields

class UrlTask(db.Model, EntityBase):
    __tablename__ = 'url_task'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    link_title = db.Column(db.String(100))
    http_head = db.Column(db.Integer)
    url = db.Column(db.String(160))
    source = db.Column(db.String(50))
    createdat = db.Column(db.Integer)
    status = db.Column(db.Integer)
    finishedat = db.Column(db.Integer)
    def __init__(self, tar_item, tar_ts):
        #self.id = id
        self.link_title = tar_item['link_title']
        self.http_head = tar_item['http_head']
        self.url = tar_item['url']
        self.source = tar_item['source']
        self.createdat = tar_ts
        self.status = 0
        self.finishedat = 0

class PageResult(db.Model, EntityBase):
    __tablename__ = 'page_result'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    content = db.Column(db.Text)
    keywords = db.Column(db.String(100))
    title = db.Column(db.String(100))
    task_id = db.Column(db.Integer, db.ForeignKey("url_task.id"))
    to_task = db.relationship("UrlTask", backref = "to_page")
    def __init__(self, tar_item):
        #self.id = id
        self.content = tar_item['content']
        self.keywords = tar_item['keywords']
        self.title = tar_item['title']
        self.task_id = tar_item['task_id']
     